
$(document).ready(function(){ 
	
	$.ajax({
	        url: "audio/click.mp3",
	        success: function() {
	            
				function fadeOut(){
			 			setTimeout(function(){
			 				$("#response").fadeOut("fast", function () {
			     			});
					}, 1000);
				}

			   	$("#response").hide();




				$(function() {
					$("#list ul").sortable({ 
						opacity: 0.8, cursor: 'move', update: function() {
							var order = $(this).sortable("serialize") + '&update=update'; 
							$('#soundclick').remove();
							$('body').append("<embed id='soundclick' src='audio/click.mp3' autostart='true' hidden='true' loop='false'>");



							$.post("updateList.php", order, function(theResponse){
								//$("#response").html(theResponse);
								$("#response").fadeIn('fast');
								fadeOut();
							}); 															 
						}								  
					});


					$(".droppable").droppable({
						hoverClass: 'droppableHover',
					      drop: function(event, ui){
								deleteImage(ui);
					}
					});


				});

				function deleteImage(ui){
					// Get id of dropped image
					var orgId = $(ui.draggable).attr('id');
					var textArray = orgId.split("_");
					var newId = textArray[textArray.length-1];


					// Delete image
					$.ajax({
						type: "POST",
					  	url: "deleteImage.php",
						data: "id="+newId,
					  success: function(){
						window.location.href = "index.php";
					  }
					});

				}
	
	
	
	
	
	
	
	        }
	    });

  	
});	
